"use strict";

var errInfo = require('./err_info.js');
var commFun = {};
module.exports = commFun;

/**
 * md5摘要
 * @param str
 */
var crypto = require('crypto');
var useMd5 = false;
commFun.md5 = function(str) {
	if (!useMd5) return str;
	var hash = crypto.createHash('md5').update(str).digest('hex');
	return hash;
};

/**
 * 格式输出
 * @param result
 * @returns {errCode: 0, result: result}
 */
commFun.formatOutput = function(result) {
	return {
		errCode: 0,
		result: result
	};
};

/**
 * 格式输出
 * @param errCode not zero, example E1001
 * @returns {errCode: *, errMsg: *,}
 */
commFun.formatError = function(err, errMsg) {
	if (!err) {
		throw new Error('unknown error:' + err);
	}
	return {
		errCode: err[0],
		errMsg: errMsg || err[1]
	}
}